<!DOCTYPE html>
<html lang="">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <link rel="icon" href="<%= BASE_URL %>favicon.ico">
  <script type="text/javascript" src="<%= BASE_URL %>./static/minxin.js"></script>
  <script type="text/javascript" src="<%= BASE_URL %>./static/SM.js"></script>
  <script type="text/javascript" src="<%= BASE_URL %>./static/jquery-3.3.1.min.js"></script>
  <script type="text/javascript" src="<%= BASE_URL %>./static/md5.js"></script>
  <script type="text/javascript" src="<%= BASE_URL %>./static/aes.js"></script>
  <link href="https://cdn.bootcdn.net/ajax/libs/blueimp-file-upload/10.32.0/css/jquery.fileupload-noscript.css"
    rel="stylesheet">

  <title><%= htmlWebpackPlugin.options.title %></title>
</head>

<body>
  <noscript>
    <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled.
      Please enable it to continue.</strong>
  </noscript>
  <div id="app"></div>
  <!-- built files will be auto injected -->
</body>
<!-- 引入爱山东JSSDK(必须) -->
<script type="text/javascript" src="<%= BASE_URL %>https://isdapp.shandong.gov.cn/jmopen/jssdk/index.js"></script>
<script>
  const appword = "aKd20dbGdFvmuwrt" // 固定值

  window.onload = function () {
    // getUserInfo()
  }

  function getUserInfo() {
    lightAppJssdk.user.getUserInfoWithEncryptedParamByAppId({

      appId: "Embmzckm", // 接入方在成功创建应用后自动生成
      success: function (data) {
        if (data == '未登录') onLoginApp()
        else {
          if (typeof data == 'string')
            data = JSON.parse(data)

          // 对于该方法的返回结果,需要进行两步解密才能得到明文用户信息,
          // 先对返回结果进行sm2解密,需要用到 接入方在创建应用时,选择应用的加密方式为sm2,并成功创建应用后,拿到的应用私钥来进行解密
          const sm2_privateKey = '6636756e69f8276c514919d04eaf928498b443bfb783e8fe6fa6977056c4d90b'
          let sm2_encrypt_result = data.data
          let sm2_decrypt_result = sm2_Decrypt(sm2_encrypt_result, sm2_privateKey)

          if (typeof sm2_decrypt_result == 'string')
            sm2_decrypt_result = JSON.parse(sm2_decrypt_result)

          // 其次,对sm2解密后的结果进行 aes解密
          // aes解密需要用到 appword , 为固定值,使用示例代码中的即可
          let aes_encrypt_result = sm2_decrypt_result.data
          let aes_decrypt_result = aes_Decrypt(aes_encrypt_result, appword)
          var dom = JSON.parse(aes_decrypt_result)
          console.log(dom);
          window.sessionStorage.setItem("mz", dom.name)
          window.sessionStorage.setItem("sjh", dom.mobile)
          window.sessionStorage.setItem("sfz", dom.papersnumber)
          window.sessionStorage.setItem("xb", dom.sex)
          // let dom1 = document.getElementById('aesData')
          // dom1.value = JSON.stringify(data)
          // let dom2 = document.getElementById('desData')
          // dom2.value = aes_decrypt_result
        }
      },
      fail: function (data) {

      }
    });
  }

  /**
   * 使用jssdk调用登录页面
   */
  function onLoginApp() {
    lightAppJssdk.user.loginapp({
      success: function (data) {
        if (data == "未登录") {
          //取消登录或登录失败，关闭页面
          oncloseWindow()
        } else {
          getUserInfo()
        }
      },
      fail: function (data) {
        //关闭页面
        oncloseWindow()
      }
    })
  }

  /**
   * 关闭容器
   */
  function oncloseWindow() {
    lightAppJssdk.navigation.close({
      success: function (data) {},
      fail: function (data) {}
    })
  }

  // aes解密
  function aes_Decrypt(word, key) {
    var key = CryptoJS.enc.Utf8.parse(key) //转为128bit
    var srcs = CryptoJS.enc.Hex.parse(word) //转为16进制
    var str = CryptoJS.enc.Base64.stringify(srcs) //变为Base64编码的字符串
    var decrypt = CryptoJS.AES.decrypt(str, key, {
      mode: CryptoJS.mode.ECB,
      spadding: CryptoJS.pad.Pkcs7
    })
    return decrypt.toString(CryptoJS.enc.Utf8)
  }

  // sm2解密,需要创建加密应用时的私钥
  function sm2_Decrypt(word, key) {
    return SM.decrypt(word, key)
  }
</script>
<!-- <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script> -->
<!-- <script>
  // VConsole will be exported to `window.VConsole` by default.
  var vConsole = new window.VConsole();
</script> -->

</html>